﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.SMECommon.DAL;
using Buffalo.SMECommon.DAL.Models;
using Buffalo.SMECommon.Enum;
using Buffalo.Lib.Common;
using Buffalo.Administration.DAL;
using Buffalo.Administration.Enum;

namespace Buffalo.Web.Areas.SMECommon.Controllers
{
    public class EmployeeController : BaseController
    {
        public ActionResult ExportExcelView()
        {
            ViewData["IDOrgUnit"] = HttpContext.Request["IDOrgUnit"] ?? string.Empty;
            ViewData["Criteria"] = HttpContext.Request["Criteria"] ?? string.Empty;
            ViewData["SearchText"] = HttpContext.Request["SearchText"] ?? string.Empty;
            return this.View();
        }

        public ActionResult UploadImageView()
        {
            return this.View();
        }
        public ActionResult EmployeeList()
        {
            return CheckPermission("Employee");
        }

        public ActionResult ImportDataView()
        {
            return this.View();
        }
        public ActionResult EmployeeEditor()
        {
            ViewData["IDEmployee"] = HttpContext.Request["IDEmployee"] ?? string.Empty;
            return this.View();
        }
        public AjaxStoreResult GetListEmployee(int limit, int start, string dir, string sort, string cboOrgUnit, string cboCriteria, string txtSearchText)
        {
            int total = 0;
            string orgUnit = cboOrgUnit.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboOrgUnit;
            string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
            string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();

            var query = new EmployeeDAL().GetListEmployee(limit, start, dir, sort, cboOrgUnit, criteria, searchText, out total);
            try
            {
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }
        public AjaxStoreResult SaveEmployees()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new EmployeeDAL().SaveEmployees(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public AjaxFormResult SaveEmployeeInfo(FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                string IDEmployee = values["id"];
                if (string.IsNullOrEmpty(values["Code"]))
                {
                    response.Success = false;
                    response.Errors.Add(new FieldError("Code", "Code is required"));
                    return response;
                }

                Employee employee;
                EmployeeDAL employeeDAL = new EmployeeDAL();
                if (string.IsNullOrEmpty(IDEmployee) || IDEmployee.Equals("0"))
                {
                    employee = new Employee();
                    employee.ID = IDEmployee;
                }
                else
                {
                    employee = employeeDAL.GetEmployee(IDEmployee);
                }
                employee.Code = values["Code"];
                employee.FirstName = values["FirstName"];
                employee.LastName = values["LastName"];
                employee.MaritalStatus = Util.ToSafeInt(values["MaritalStatus"]);
                employee.DOB = Util.ToSafeDateTime(values["DOB"]);

                employeeDAL.SaveEmployee(employee);

                response.ExtraParams["newID"] = employee.ID;
            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.Message;
            }

            return response;
        }
        public AjaxStoreResult GetEmployee(string IDEmployee)
        {
            if (!string.IsNullOrEmpty(IDEmployee))
            {
                if (IDEmployee.Equals("0"))
                {
                    var query = new Employee();
                    query.ID = "0";
                    return new AjaxStoreResult(query, 1);
                }
                else
                {
                    var query = new EmployeeDAL().GetEmployee(IDEmployee);
                    int total = query != null ? 1 : 0;
                    return new AjaxStoreResult(query, total);
                }
            }

            return null;
        }

        public AjaxStoreResult SaveEmployeeFamilies()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new EmployeeDAL().SaveEmployeeFamilies(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public AjaxStoreResult GetListEmployeeFamily(int limit, int start, string dir, string sort, string IDEmployee)
        {
            int total = 0;
            try
            {
                var query = new EmployeeDAL().GetListEmployeeFamily(limit, start, dir, sort, out total, IDEmployee);
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }

        private ActionResult CheckPermission(string featureName)
        {
            if (!HttpContext.User.Identity.IsAuthenticated)
                return this.RedirectToAction("ReLogin", "Home");

            bool hasViewPermision = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, featureName, enuPermissionType.View);
            if (hasViewPermision)
                return this.View();
            return null;
        }


    }
}
